<script setup lang="ts">
import Course from './components/Course.vue';
import Article from './components/Article.vue';
import Module from './components/Module.vue';
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const banners = ref([
  { id: 1, image: '0.jpg', title: 'WitData学习平台', link: '/home' },
  // 可以在后台动态添加更多轮播图
]);

const navigateTo = (path: string) => {
  router.push(path);
};

// 推荐的特色功能
const features = [
  { 
    id: 1, 
    title: '知识图谱', 
    icon: 'cluster-o', 
    color: '#1989fa',
    path: '/knowledge-graph',
    description: '基于知识图谱的智能问答，助你快速掌握知识' 
  },
  { 
    id: 2, 
    title: '智能编程', 
    icon: 'edit', 
    color: '#ff6b6b',
    path: '/user/code/list',
    description: '随时随地编写、运行和调试代码，无需搭建环境' 
  },
  { 
    id: 3, 
    title: '刷题测验', 
    icon: 'passed', 
    color: '#07c160',
    path: '/subject',
    description: '丰富的编程题目库，助你掌握核心算法与技能' 
  },
  { 
    id: 4, 
    title: '专业课程', 
    icon: 'flower-o', 
    color: '#ee0a24',
    path: '/classroom',
    description: '系统化学习路径，从入门到精通的专业指导' 
  }
];

// 处理banner点击
const handleBannerClick = (link: string) => {
  if (link) router.push(link);
};
</script>

<template>
  <div class="home-container">
    <!-- 主Banner区域 -->
    <div class="banner-section">
      <van-swipe 
        class="banner-swipe" 
        :autoplay="4000" 
        :show-indicators="true"
        indicator-color="#1989fa"
        :loop="true"
        :touchable="true"
      >
        <van-swipe-item v-for="banner in banners" :key="banner.id" @click="handleBannerClick(banner.link)">
          <div class="banner-item">
            <img :src="banner.image" :alt="banner.title">
            <div class="banner-overlay">
              <div class="banner-title">{{ banner.title }}</div>
            </div>
          </div>
        </van-swipe-item>
      </van-swipe>
    </div>
    
    <!-- 特色功能区 -->
    <div class="features-section">
      <h2 class="section-title">特色功能</h2>
      <div class="feature-grid">
        <div 
          v-for="feature in features" 
          :key="feature.id" 
          class="feature-card hover-card"
          @click="navigateTo(feature.path)"
        >
          <div class="feature-icon" :style="{ backgroundColor: `${feature.color}15` }">
            <van-icon :name="feature.icon" :color="feature.color" size="28" />
          </div>
          <div class="feature-content">
            <h3>{{ feature.title }}</h3>
            <p>{{ feature.description }}</p>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 快速导航模块 -->
    <div class="module-section">
      <h2 class="section-title">快速导航</h2>
      <Module />
    </div>
    
    <!-- 课程推荐 -->
    <div class="course-section">
      <div class="section-header">
        <h2 class="section-title">精选课程</h2>
        <a class="view-more" @click="navigateTo('/classroom')">
          查看全部 <van-icon name="arrow" />
        </a>
      </div>
      <Course />
    </div>
    
    <!-- 文章推荐 -->
    <!-- <div class="article-section">
      <div class="section-header">
        <h2 class="section-title">热门文章</h2>
        <a class="view-more" @click="navigateTo('/community')">
          查看全部 <van-icon name="arrow" />
        </a>
      </div>
      <Article />
    </div> -->
  </div>
</template>

<style lang="scss" scoped>
.home-container {
  display: flex;
  flex-direction: column;
  gap: 30px;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  
  .banner-section {
    margin-bottom: 10px;
    
    .banner-swipe {
      height: 280px;
      border-radius: 12px;
      overflow: hidden;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
      
      .banner-item {
        position: relative;
        width: 100%;
        height: 100%;
        
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
        
        .banner-overlay {
          position: absolute;
          bottom: 0;
          left: 0;
          right: 0;
          padding: 20px;
          background: linear-gradient(to top, rgba(0,0,0,0.7), transparent);
          
          .banner-title {
            color: white;
            font-size: 24px;
            font-weight: bold;
          }
        }
      }
    }
  }
  
  .section-title {
    font-size: 20px;
    font-weight: 600;
    color: #323233;
    margin: 0 0 16px 0;
  }
  
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    
    .view-more {
      display: flex;
      align-items: center;
      color: #1989fa;
      font-size: 14px;
      cursor: pointer;
      
      .van-icon {
        margin-left: 4px;
      }
      
      &:hover {
        opacity: 0.8;
      }
    }
  }
  
  .features-section {
    .feature-grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
      gap: 16px;
      
      .feature-card {
        background-color: #fff;
        border-radius: 12px;
        padding: 20px;
        display: flex;
        flex-direction: column;
        align-items: center;
        cursor: pointer;
        transition: all 0.3s ease;
        text-align: center;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        
        .feature-icon {
          width: 64px;
          height: 64px;
          border-radius: 32px;
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: 16px;
        }
        
        .feature-content {
          h3 {
            margin: 0 0 8px;
            font-size: 16px;
            font-weight: 600;
            color: #323233;
          }
          
          p {
            margin: 0;
            font-size: 14px;
            color: #646566;
            line-height: 1.5;
          }
        }
      }
    }
  }
  
  .module-section {
    margin-bottom: 10px;
  }
  
  .course-section, .article-section {
    background-color: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  }
}

// 响应式适配
@media (max-width: 768px) {
  .home-container {
    gap: 20px;
    padding: 15px;
    
    .banner-section .banner-swipe {
      height: 200px;
      
      .banner-item .banner-overlay .banner-title {
        font-size: 20px;
      }
    }
    
    .features-section .feature-grid {
      grid-template-columns: repeat(2, 1fr);
      
      .feature-card {
        padding: 16px;
        
        .feature-icon {
          width: 50px;
          height: 50px;
        }
        
        .feature-content h3 {
          font-size: 15px;
        }
        
        .feature-content p {
          font-size: 12px;
        }
      }
    }
  }
}

@media (max-width:
       480px) {
  .home-container {
    gap: 15px;
    padding: 10px;
    
    .banner-section .banner-swipe {
      height: 160px;
      
      .banner-item .banner-overlay .banner-title {
        font-size: 18px;
      }
    }
    
    .features-section .feature-grid {
      grid-template-columns: 1fr;
    }
    
    .section-title {
      font-size: 18px;
    }
  }
}
</style>